/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.network.clientpackets;

import jmmos.Config;
import jmmos.gameserver.ai.CtrlIntention;
import jmmos.gameserver.datatables.SkillTable;
import jmmos.gameserver.model.MMOCharPosition;
import jmmos.gameserver.model.actor.instance.MMOPcInstance;
import jmmos.gameserver.model.actor.position.PcPosition;
import jmmos.gameserver.model.holders.SkillHolder;
import jmmos.gameserver.model.skills.MMOSkill;
import jmmos.gameserver.model.skills.MMOSkillType;
import jmmos.gameserver.model.skills.targets.MMOTargetType;
import jmmos.gameserver.network.serverpackets.ActionFailed;

/**
 * This class ...
 * @version $Revision: 1.7.2.1.2.3 $ $Date: 2005/03/27 15:29:30 $
 */
public final class RequestMagicSkillUse extends MMOGameClientPacket
{
	private static final String _C__39_REQUESTMAGICSKILLUSE = "[C] 39 RequestMagicSkillUse";
	
	private int _magicId;
	private boolean _ctrlPressed;
	private boolean _shiftPressed;
	
	@Override
	protected void readImpl()
	{
		_magicId = readD(); // Identifier of the used skill
		_ctrlPressed = readD() != 0; // True if it's a ForceAttack : Ctrl pressed
		_shiftPressed = readC() != 0; // True if Shift pressed
	}
	
	@Override
	protected void runImpl()
	{
		// Get the current MMOPcInstance of the player
		final MMOPcInstance activeChar = getClient().getActiveChar();
		if (activeChar == null)
		{
			return;
		}
		
		// Get the level of the used skill
		int level = activeChar.getSkillLevel(_magicId);
		if (level <= 0)
		{
			// Player doesn't know this skill, maybe it's the display Id.
			final SkillHolder customSkill = activeChar.getCustomSkills().get(_magicId);
			if (customSkill != null)
			{
				_magicId = customSkill.getSkillId();
				level = customSkill.getSkillLvl();
			}
			else
			{
				activeChar.sendPacket(ActionFailed.STATIC_PACKET);
				return;
			}
		}
		
		// Get the MMOSkill template corresponding to the skillID received from the client
		MMOSkill skill = SkillTable.getInstance().getInfo(_magicId, level);
		
		// Check the validity of the skill
		if (skill != null)
		{
			if ((activeChar.isTransformed() || activeChar.isInStance()) && !activeChar.containsAllowedTransformSkill(skill.getId()))
			{
				activeChar.sendPacket(ActionFailed.STATIC_PACKET);
				return;
			}
			
			if (Config.DEBUG)
			{
				_log.fine("	skill:" + skill.getName() + " level:" + skill.getLevel() + " passive:" + skill.isPassive());
				_log.fine("	range:" + skill.getCastRange() + " targettype:" + skill.getTargetType() + " power:" + skill.getPower());
				_log.fine("	reusedelay:" + skill.getReuseDelay() + " hittime:" + skill.getHitTime());
			}
			
			// If Alternate rule Karma punishment is set to true, forbid skill Return to player with Karma
			if ((skill.getSkillType() == MMOSkillType.RECALL) && !Config.ALT_GAME_KARMA_PLAYER_CAN_TELEPORT && (activeChar.getKarma() > 0))
			{
				return;
			}
			
			// players mounted on pets cannot use any toggle skills
			if (skill.isToggle() && activeChar.isMounted())
			{
				return;
			}
			
			activeChar.useMagic(skill, _ctrlPressed, _shiftPressed);
			
			// Stop if use self-buff (except if on AirShip or Boat).
			if (((skill.getSkillType() == MMOSkillType.BUFF) && (skill.getTargetType() == MMOTargetType.TARGET_SELF)) && (!activeChar.isInAirShip() || !activeChar.isInBoat()))
			{
				final PcPosition charPos = activeChar.getPosition();
				final MMOCharPosition stopPos = new MMOCharPosition(charPos.getX(), charPos.getY(), charPos.getZ(), charPos.getHeading());
				activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, stopPos);
			}
		}
		else
		{
			activeChar.sendPacket(ActionFailed.STATIC_PACKET);
			_log.warning("No skill found with id " + _magicId + " and level " + level + " !!");
		}
	}
	
	@Override
	public String getType()
	{
		return _C__39_REQUESTMAGICSKILLUSE;
	}
}
